package edu.scu.declaresys.service.impl;

import edu.scu.declaresys.config.Configs;
import edu.scu.declaresys.dao.DAOFactory;
import edu.scu.declaresys.dao.intr.IProjectDAO;
import edu.scu.declaresys.dao.util.Depth;
import edu.scu.declaresys.dao.util.StateUtils;
import edu.scu.declaresys.exception.ExecuteException;
import edu.scu.declaresys.service.intr.IProjectService;
import edu.scu.declaresys.util.PageList;
import edu.scu.declaresys.vo.Area;
import edu.scu.declaresys.vo.Project;
import edu.scu.declaresys.vo.ProjectClass;

public class ProjectServiceImpl implements IProjectService
{
	private IProjectDAO dao = DAOFactory.createProjectDAO();
	
	//
	// 通用
	//
	@Override
	public Project findById(Integer id, int depth) throws ExecuteException
	{
		return dao.findById(id, depth);
	}

	@Override
	public void update(Project project) throws ExecuteException
	{
		dao.update(project, project.getId());
		return ;
	}
	
	@Override
	public Integer addProject(Project project) throws ExecuteException
	{
		return dao.save(project);
	}

	//
	// 项目申报
	//
//	@Override
//	public Project saveDraft(Project project) throws ExecuteException
//	{
//		project.setState(Project.STATE_DRAFT);
//		project.setId(dao.save(project));
//		return project;
//	}
//
//	@Override
//	public void deleteDraft(Integer id) throws ExecuteException
//	{
//		dao.delete(id);
//		return ;
//	}

	
	
//	@Override
//	public void commitProject(Integer id) throws ExecuteException
//	{
//		Project vo = dao.findById(id, Depth.ONE);
//		if(vo.getLevel() != null)
//		{			
//			vo.setState(vo.getLevel() == Area.LEVEL_COUNTY ? Project.STATE_UNRCMD_COUNTY : Project.STATE_UNRCMD_CITY);
//		}
//		dao.update(vo, vo.getId());
//		return ;
//	}

	//
	//
	//
//	@Override
//	public PageList<Project> getMyCommittedProject(Integer userId, Integer pageNum, String orderColumn, Boolean isAsc)
//			throws ExecuteException
//	{
//		return dao.findByExcludeStates(userId,
//				new Integer[]{Project.STATE_DRAFT},
//				Depth.ONE,
//				pageNum == null ? 1 : pageNum,
//				Configs.PROJECT_AMT_PER_PAGE,
//				orderColumn,
//				isAsc);
//	}

	@Override
	public Project findByNameUnique(String name) throws ExecuteException
	{
		return dao.findByPropertyUnique(Project.T_NAME, name, Depth.ONE);
	}

	@Override
	public PageList<Project> getMyCommittedProject(Integer userId, Integer projectClassId, Integer state, String projectName,
			Integer page, String orderColumn, Boolean isAsc) throws ExecuteException
	{
		Integer[] states = null;
		if(state != null && !state.equals(Project.STATE_NULL))
		{
			states = new Integer[]{state};
		}
		if(projectClassId != null && projectClassId.equals(ProjectClass.CLASS_NULL))
		{
			projectClassId = null;
		}
		return dao.findProject(userId, null, null, null, null, 
				states, null, projectClassId, projectName,
				Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE, Project.T_ID, true);
	}

	//
	// 项目推荐
	//
	@Override
	public PageList<Project> findWaitRcmdProjectList(Integer rcmdUserId, int level,
			Integer projectClassId, Integer year, String projectName, Integer page)
	throws ExecuteException
	{
		PageList<Project> voLst = null;
		if(year != null && year.equals(Project.YEAR_NULL))
		{
			year = null;
		}
		if(projectClassId != null && projectClassId.equals(ProjectClass.CLASS_NULL))
		{
			projectClassId = null;
		}
		if(level == Area.LEVEL_COUNTY)
		{
			voLst = dao.findProject(null,
					rcmdUserId, null, null,
					null,
					new Integer[]{Project.STATE_UNRCMD_COUNTY},
					year, projectClassId, projectName, 
					Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE,
					Project.T_ID, true);
		}
		else if(level == Area.LEVEL_CITY)
		{
			voLst = dao.findProject(null,
					null, rcmdUserId, null,
					null,
					new Integer[]{Project.STATE_UNRCMD_CITY, Project.STATE_COUNTY_REQUEST_CANCEL_RCMD},
					year, projectClassId, projectName,
					Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE,
					Project.T_ID, true);
		}
		else if(level == Area.LEVEL_PROVINCE)
		{
			voLst = dao.findProject(null,
					null, null, rcmdUserId,
					null,
					new Integer[]{Project.STATE_UNRCMD_PROVINCE, Project.STATE_CITY_REQUEST_CANCEL_RCMD},
					year, projectClassId, projectName,
					Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE,
					Project.T_ID, true);
		}
		if(voLst == null)
		{
			voLst = new PageList<Project>();
		}
		return voLst;
	}

	@Override
	public PageList<Project> findRcmdedProjectList(Integer rcmdUserId, Integer level,
			Integer projectClassId, Integer year, Integer state, String projectName,
			Integer page) 
	throws ExecuteException
	{
		PageList<Project> voLst = null;
		if(year != null && year.equals(Project.YEAR_NULL))
		{
			year = null;
		}
		if(projectClassId != null && projectClassId.equals(ProjectClass.CLASS_NULL))
		{
			projectClassId = null;
		}
		if(state != null && !state.equals(Project.STATE_NULL))
		{
			state = null;
		}
		if(level == Area.LEVEL_COUNTY)
		{
			voLst = dao.findProject(null,
					rcmdUserId, null, null,
					null,
					StateUtils.getStatesExcept(new Integer[]{Project.STATE_UNRCMD_COUNTY, state}),
					year, projectClassId, projectName,
					Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE,
					Project.T_ID, true);
		}
		else if(level == Area.LEVEL_CITY)
		{
			voLst = dao.findProject(null,
					null, rcmdUserId, null,
					null,
					StateUtils.getStatesExcept(new Integer[]{Project.STATE_UNRCMD_CITY, Project.STATE_COUNTY_REQUEST_CANCEL_RCMD, state}),
					year, projectClassId, projectName,
					Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE,
					Project.T_ID, true);
		}
		else if(level == Area.LEVEL_PROVINCE)
		{
			voLst = dao.findProject(null,
					null, null, rcmdUserId,
					null,
					StateUtils.getStatesExcept(new Integer[]{Project.STATE_UNRCMD_PROVINCE, Project.STATE_CITY_REQUEST_CANCEL_RCMD, state}),
					year, projectClassId, projectName,
					Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE,
					Project.T_ID, true);
		}
		if(voLst == null)
		{
			voLst = new PageList<Project>();
		}
		return voLst;
	}

	@Override
	public PageList<Project> findCancelRcmdProject(Integer rcmdUserId, Integer level, Integer projectClassId,
			Integer year, String projectName, Integer page) 
	throws ExecuteException
	{
		PageList<Project> voLst = null;
		if(year != null && year.equals(Project.YEAR_NULL))
		{
			year = null;
		}
		if(projectClassId != null && projectClassId.equals(ProjectClass.CLASS_NULL))
		{
			projectClassId = null;
		}
		if(level == Area.LEVEL_CITY)
		{
			voLst = dao.findProject(null,
					null, rcmdUserId, null,
					null,
					new Integer[]{Project.STATE_COUNTY_REQUEST_CANCEL_RCMD},
					year, projectClassId, projectName,
					Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE,
					Project.T_ID, true);
		}
		else if(level == Area.LEVEL_PROVINCE)
		{
			voLst = dao.findProject(null,
					null, null, rcmdUserId,
					null,
					new Integer[]{Project.STATE_CITY_REQUEST_CANCEL_RCMD},
					year, projectClassId, projectName,
					Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE,
					Project.T_ID, true);
		}
		if(voLst == null)
		{
			voLst = new PageList<Project>();
		}
		return voLst;
	}

	//
	// 项目审批
	//
	@Override
	public PageList<Project> findWaitAuditProject(Integer projectClassId, Integer year, String name, Integer page)
			throws ExecuteException
	{
		if(year != null && year.equals(Project.YEAR_NULL))
		{
			year = null;
		}
		if(projectClassId != null && projectClassId.equals(ProjectClass.CLASS_NULL))
		{
			projectClassId = null;
		}
		return dao.findProject(null, null, null, null, null,
						new Integer[]{Project.STATE_REQUEST_MODIFY, Project.STATE_UNAUDIT_MODIFIABLE, Project.STATE_UNAUDIT_UNMODIFIABLE},
						year, projectClassId, name, 
						Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE, Project.T_ID, true);
	}

	@Override
	public PageList<Project> findAudittedProject(Integer projectClassId, Integer year, String name, Integer page)
			throws ExecuteException
	{
		if(year != null && year.equals(Project.YEAR_NULL))
		{
			year = null;
		}
		if(projectClassId != null && projectClassId.equals(ProjectClass.CLASS_NULL))
		{
			projectClassId = null;
		}
		return dao.findProject(null, null, null, null, null,
				new Integer[]{Project.STATE_PASSED, Project.STATE_REJECTED},
				year, projectClassId, name, 
				Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE, Project.T_ID, true);
	}

	@Override
	public PageList<Project> findRequestModifyProject(Integer projectClassId, Integer year, String name, Integer page)
	throws ExecuteException
	{
		if(year != null && year.equals(Project.YEAR_NULL))
		{
			year = null;
		}
		if(projectClassId != null && projectClassId.equals(ProjectClass.CLASS_NULL))
		{
			projectClassId = null;
		}
		return dao.findProject(null, null, null, null, null,
				new Integer[]{Project.STATE_REQUEST_MODIFY},
				year, projectClassId, name, 
				Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE, Project.T_ID, true);
	}

	//
	// 项目检索
	//
	@Override
	public PageList<Project> searchProject(Project project, Integer page) throws ExecuteException
	{
		Integer projectClassId = project.getProjectClassId();
		Integer year = project.getYear();
		if(year != null && year.equals(Project.YEAR_NULL))
		{
			year = null;
		}
		if(projectClassId != null && projectClassId.equals(ProjectClass.CLASS_NULL))
		{
			project.setProjectClassId(null);
		}
		if(page == null)
		{
			page = 1;
		}
		return dao.searchProject(project.getAreaCityId(), project.getAreaCountyId(), project.getDeclareUserId(), project.getProjectClassId(), 
				project.getYear(), project.getName(), project.getState(), Depth.ONE, page, Configs.PROJECT_AMT_PER_PAGE, Project.T_ID, true);
	}

	//
	//
	//
	@Override
	public boolean hasProjectInArea(Integer id) throws ExecuteException
	{
		Area area = DAOFactory.createAreaDAO().findById(id, Depth.ONE);
		if(area.getLevel().equals(Area.LEVEL_COUNTY))
		{
			return dao.searchProject(null, area.getLevel(),
					null, null, null, null, null, Depth.ONE, null, null, null, null).isEmpty() == false;
		}
		else if(area.getLevel().equals(Area.LEVEL_CITY))
		{
			return dao.searchProject(area.getLevel(), null,
					null, null, null, null, null, Depth.ONE, null, null, null, null).isEmpty() == false;
		}
		return true;
	}

	
	
	
}
